﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZedGraph;
using System.Configuration;

namespace FileOperationWinform
{
    public partial class FrmMap : Form
    {
        private static readonly string skin=ConfigurationManager.AppSettings["Skin"];
        public FrmMap()
        {
            InitializeComponent();
        }
        private int pdf = 0;
        private int zip = 0;
        private int rar = 0;
        private int microsoft2003 = 0;
        private int microsoft2007 = 0;
        private int wps = 0;
        private int Rest=0;
        private int CountNum = 0;
        private DataTable maplist;
        public FrmMap(FormMain.recordflag r)
        {
            InitializeComponent();
            this.pdf = r.pdf;
            this.zip = r.zip;
            this.rar = r.rar;
            this.microsoft2003 = r.microsoft2003;
            this.microsoft2007 = r.microsoft2007;
            this.wps = r.wps;
            this.Rest=r.Rest;
            this.CountNum = r.CountNum;
            this.maplist = r.recordList;
        }
        private ZedGraph.ZedGraphControl z1=new ZedGraph.ZedGraphControl();
        protected GraphPane myPane;

        public void Createchar1(ZedGraphControl zgr)
        {
            myPane = zgr.GraphPane;

            myPane.Title = "文件统计分析图";
            myPane.FontSpec.IsItalic = true;
            myPane.FontSpec.Size = 12f;
            //myPane.FontSpec.Family = "Times";
            //myPane.Chart.Fill.Type = FillType.None;

            myPane.PaneFill = new Fill(Color.White, Color.Goldenrod, 65.0f);
            // No fill for the axis background
            myPane.AxisFill.Type = FillType.None;

            // Set the legend to an arbitrary location
            myPane.Legend.Position = LegendPos.Float;
            myPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,
                                AlignH.Right, AlignV.Center);
            myPane.Legend.FontSpec.Size = 8f;
            myPane.Legend.IsHStack = false;

            //double [] values2 =   { 250, 50, 900, 50 } ;
            //Color [] colors = { Color.Red, Color.Blue, Color.Green, Color.Yellow } ;
            //double [] displacement = {	.0,.0,.0,.0 } ;
            //string [] labels = {"pdf文件", "Rar文件", "Zip文件", "Wps文件" } ; 

			// Fill the pane and axis background with solid color
            //myPane.PaneFill = new Fill( Color.Cornsilk );
            //myPane.AxisFill = new Fill( Color.Cornsilk );
			myPane.Legend.Position = LegendPos.Right ;
			
			// Create some pie slices
            if (this.pdf != 0)
            {
                PieItem segment_1 = myPane.AddPieSlice(this.pdf, Color.Navy, Color.White, 45f, 0, "Pdf");
                segment_1.LabelType = PieLabelType.Name_Percent;
            }
            if (this.rar != 0)
            {
                PieItem segment_2 = myPane.AddPieSlice(this.rar, Color.Salmon, Color.White, 45f, 0, "Rar");
                segment_2.LabelType = PieLabelType.Name_Percent;
                segment_2.LabelDetail.FontSpec.FontColor = Color.Red;
            }
            if (this.zip != 0)
            {
                PieItem segment_3 = myPane.AddPieSlice(this.zip, Color.Yellow, Color.White, 45f, 0, "Zip");
                segment_3.LabelType = PieLabelType.Name_Percent;
            }
            if (this.wps != 0)
            {
                PieItem segment_4 = myPane.AddPieSlice(this.wps, Color.LimeGreen, Color.White, 45f, 0, "Wps");
                segment_4.LabelType = PieLabelType.Name_Percent;
            }
            if (this.microsoft2003 != 0)
            {
                PieItem segment_5 = myPane.AddPieSlice(this.microsoft2003, Color.Cyan, Color.White, 45f, 0, "Microsoft 2003");
                segment_5.LabelType = PieLabelType.Name_Percent;
            }
            if (this.microsoft2007 != 0)
            {
                PieItem segment_6 = myPane.AddPieSlice(this.microsoft2007, Color.DarkGreen, Color.White, 45f, 0, "Microsoft 2007");
                segment_6.LabelType = PieLabelType.Name_Percent;
            }
            if (this.Rest != 0)
            {
                PieItem segment_7 = myPane.AddPieSlice(10.5, Color.DodgerBlue, Color.White, 45f, 0, "Rest");
                segment_7.LabelType = PieLabelType.Name_Percent;
            }

            for (int i = 0; i < this.maplist.Rows.Count; i++)
            {
                //string sname = "segment" + Convert.ToString(i);
                PieItem segment_8 = myPane.AddPieSlice(Convert.ToUInt32(this.maplist.Rows[i][2]), GetRandomColor(100 + i), Color.White, 45f, 0, Convert.ToString(this.maplist.Rows[i][1]));
                segment_8.LabelType = PieLabelType.Name_Percent;
            }
			
			// Add some more slices as an array
            //PieItem [] slices = new PieItem[values2.Length] ;
            //slices = myPane.AddPieSlices ( values2, labels ) ;

            //// Modify the slice label types
            //((PieItem)slices[0]).LabelType = PieLabelType.Name_Value ;
            //((PieItem)slices[1]).LabelType = PieLabelType.Name_Value_Percent ;
            //((PieItem)slices[2]).LabelType = PieLabelType.Name_Value ;
            //((PieItem)slices[3]).LabelType = PieLabelType.Name_Value ;
            //((PieItem)slices[1]).Displacement = .2 ;
			
			
			// Sum up the values																					
			CurveList curves = myPane.CurveList ;
			double total = 0 ;
			for ( int x = 0 ; x <  curves.Count ; x++ )
				total += ((PieItem)curves[x]).Value ;

			// Add a text item to highlight total sales
			TextItem text = new TextItem("总共搜索文件" + "Num:" + this.CountNum.ToString () + "个", 0.90F, 0.80F,CoordType.PaneFraction );
			text.Location.AlignH = AlignH.Center;
			text.Location.AlignV = AlignV.Bottom;
			text.FontSpec.Border.IsVisible = false ;
			text.FontSpec.Fill = new Fill( Color.White, Color.PowderBlue, 35F );
			text.FontSpec.StringAlignment = StringAlignment.Center ;
			myPane.GraphItemList.Add( text );
			
			// Add a colored background behind the pie
			BoxItem box = new BoxItem( new RectangleF( 0F, 0F, 1F, 1F ),
				Color.Empty, Color.PeachPuff );
			box.Location.CoordinateFrame = CoordType.AxisFraction;
			box.Border.IsVisible = false;
			box.Location.AlignH = AlignH.Left;
			box.Location.AlignV = AlignV.Top;
			box.ZOrder = ZOrder.E_BehindAxis;
			myPane.GraphItemList.Add( box );
            zgr.AxisChange();
        }
        private static Color GetRandomColor(int seed)
        {
            Random random = new Random(seed);
            int r = 0;
            int g = 0;
            int b = 0;
            r = random.Next(0, 230);
            g = random.Next(0, 230);
            b = random.Next(0, 235);
            Color randomcolor = Color.FromArgb(r, g, b);
            return randomcolor;
        }
        private string getcolor()
        {

            Random RandomNum_First = new Random(Guid.NewGuid().GetHashCode());
            //Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
            // 对于C#的随机数，没什么好说的 
            System.Threading.Thread.Sleep(RandomNum_First.Next(10));
            Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);

            // 为了在白色背景上显示，尽量生成深色 
            int int_Red = RandomNum_First.Next(254);
            int int_Green = RandomNum_Sencond.Next(254);
            int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
            int_Blue = (int_Blue > 255) ? 255 : int_Blue;
            return Color.FromArgb(int_Red, int_Green, int_Blue).Name;
        }
        public void Createchar(ZedGraphControl zgr)
        {
            myPane = zgr.GraphPane;
            myPane.Title = "文件统计分析图";
            myPane.FontSpec.IsItalic = true;
            myPane.FontSpec.Size = 24f;
            myPane.FontSpec.Family = "Times";

            myPane.PaneFill = new Fill(Color.White, Color.Goldenrod, 45.0f);
            // No fill for the axis background
            myPane.AxisFill.Type = FillType.None;

            // Set the legend to an arbitrary location
            myPane.Legend.Position = LegendPos.Float;
            myPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,
                                AlignH.Right, AlignV.Top);
            myPane.Legend.FontSpec.Size = 10f;
            myPane.Legend.IsHStack = false;

            PieItem segment1 = myPane.AddPieSlice(this.pdf, Color.Navy, Color.White, 45f, 0, "pdf");
            PieItem segment3 = myPane.AddPieSlice(this.zip, Color.Purple, Color.White, 45f, .0, "zip");
            PieItem segment4 = myPane.AddPieSlice(this.rar, Color.LimeGreen, Color.White, 45f, 0, "rar");
            PieItem segment2 = myPane.AddPieSlice(400, Color.SandyBrown, Color.White, 45f, 0.2, "rest");
            PieItem segment6 = myPane.AddPieSlice(this.wps, Color.Red, Color.White, 45f, 0, "wps");
            PieItem segment7 = myPane.AddPieSlice(this.microsoft2007, Color.Blue, Color.White, 45f, 0.2, "Microsoft 2003");
            PieItem segment8 = myPane.AddPieSlice(this.microsoft2007, Color.Green, Color.White, 45f, 0, "Microsoft 2007");
            //PieItem segment9 = myPane.AddPieSlice(50, Color.Yellow, Color.White, 45f, 0.2, "Africa");

            segment2.LabelDetail.FontSpec.FontColor = Color.Red;

            // Sum up the pie values																					
            CurveList curves = myPane.CurveList;
            double total = 0;
            for (int x = 0; x < curves.Count; x++)
                total += ((PieItem)curves[x]).Value;

            // Make a text label to highlight the total value
            TextItem text = new TextItem("Total 2004 Sales\n" + "$" + total.ToString() + "M",
                                0.18F, 0.40F, CoordType.PaneFraction);
            text.Location.AlignH = AlignH.Center;
            text.Location.AlignV = AlignV.Bottom;
            text.FontSpec.Border.IsVisible = false;
            text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F);
            text.FontSpec.StringAlignment = StringAlignment.Center;
            myPane.GraphItemList.Add(text);

            // Create a drop shadow for the total value text item
            TextItem text2 = new TextItem(text);
            text2.FontSpec.Fill = new Fill(Color.Black);
            text2.Location.X += 0.008f;
            text2.Location.Y += 0.01f;
            myPane.GraphItemList.Add(text2);

            zgr.AxisChange();


        }
        private void FrmMap_Load(object sender, EventArgs e)
        {
            //zedGraphControl1.IsShowPointValues = true;
            //zedGraphControl1.GraphPane.Title = "文件统计分析图";
            //double[] x = new double[100];
            //double[] y = new double[100];
            int i;

            FileOpera ft = new FileOpera();
            string st = ft.SkinLoad();
            if (!(string.Equals(st, "Error")))
            {
                skinEngine1.SkinFile = System.IO.Path.Combine(Application.StartupPath, st);
            }

            zedGraphControl1.IsEnableHPan = true;
            zedGraphControl1.IsEnableVPan = true;
            zedGraphControl1.IsEnableZoom = true;
            Createchar1(zedGraphControl1);
            //Createchar(zedGraphControl1);

            //this.z1.Name = "zedGraphControl1";
            //this.z1.Size = new System.Drawing.Size(680, 414);
            //this.z1.TabIndex = 0;


     
            //for (i = 0; i < 100; i++)
            //{
            //    x[i] = (double)i / 100.0 * Math.PI * 2.0;
            //    y[i] = Math.Sin(x[i]);
            //}
            //zedGraphControl1.GraphPane.AddCurve("Sine Wave", x, y, Color.Red, SymbolType.Circle);
            //myPane.AxisChange(null);
            //zedGraphControl1.AxisChange();
            //zedGraphControl1.Invalidate();
        }
    }
}
